#include "test.h"
#include "stdio.h"
#include "stdlib.h"
#include "KEY.h"

extern USARTDATA   Uart1;
extern unsigned char USART1_RXBuffer[200];
unsigned char Int_flag = 0;

/*********************************************************************************************************
初始化模块：跑马灯、继电器模拟测试
*********************************************************************************************************/
void LED_test(void)
{        
    lcd_clear(Black);
	  LCD_DrawRectangle(0,0, 479, 271, Green);
	  LCD_DrawRectangle(5,5, 474, 266, Green);
	  LCD_ShowString(175, 50, STR_SMART_HOME, White, Black);	
  	LCD_ShowString(140, 100, STR_AUTHOR_NAMES, Red, Black); 	
  	LCD_ShowString(175, 220, STR_WAITING, White, Black); 
    LatticeGpio_Init();//点阵初始化
    // 等待人体红外感应模块检测到有人
    while(1)
    {
        if (HUMIRIN() == 1) // 假设HUMIRIN()函数返回1表示检测到人
        {
              LCD_ShowString(100, 220, STR_S5_NEXT_MODULE, White, Black);
            break; // 退出等待循环，开始跑马灯和继电器操作
        }
    }

    while(1)
    {

				HC595SendData(0x00, 0x00, 0xFF, 0xFF);                              //  全亮
        LED4_ON;
        LED3_OFF;
        LED2_OFF;
        LED1_OFF;
        delay_ms(100);
        
        LED4_OFF;
        LED3_ON;
        LED2_OFF;
        LED1_OFF;
        delay_ms(100);
        
        LED4_OFF;
        LED3_OFF;
        LED2_ON;
        LED1_OFF;
        delay_ms(100);
        
        LED4_OFF;
        LED3_OFF;
        LED2_OFF;
        LED1_ON;
        delay_ms(100);
				
				RelayGpio_Init();													//	初始化继电器引脚
    
				RELAY1_ON;                                
				delay_ms(500);
				RELAY1_OFF;
    
				RELAY2_ON;
				delay_ms(500);
				RELAY2_OFF;
    
				RELAY3_ON;
				delay_ms(500);
				RELAY3_OFF;
				

		if (KeyScan() == 5)
      {
            delay_ms(500);    
				
            return;    
        
      }          
    }
}

/*********************************************************************************************************
自动响应模块：响应检测有人回家时，智能家居系统初始化
*********************************************************************************************************/
void Gas_Hum_test(void)
{
    unsigned char value1 = 0;
            
    lcd_clear(Black);
	  LCD_DrawRectangle(0,0, 479, 271, Green);
	  LCD_DrawRectangle(5,5, 474, 266, Green);
  	LCD_ShowString(175, 50, STR_SMART_HOME, White, Black);		
	  LCD_ShowString(150, 100, STR_HUMAN_DETECT_TITLE, Red, Black);	

    HumIRGpio_Init();													//	人体红外引脚初始化
    
	  while(1)
	  {
        value1 = HUMIRIN();
        if ( value1== 1)
        {
            LCD_ShowString(200, 180, STR_WELCOME_HOME, White, Black);
					  DiplayChineseCharacters();                                          //  显示汉字
						return;
        }
        else
        {
            LCD_ShowString(200, 180, STR_WAITING_STATUS, White, Black);
        }
	  }
}

/*********************************************************************************************************
自动窗帘模块：利用光照度传感器和步进电机，实现窗帘在强光时自动闭合避光
*********************************************************************************************************/
void Illuminance_test(void)
{        
    unsigned int date = 0;
	  unsigned int temp = 0; 
    unsigned char Databuf[3] = {0};
    char Displaybuf[20] = {0};
    int lux;
    
    lcd_clear(Black);
	  LCD_DrawRectangle(0,0, 479, 271, Green);
	  LCD_DrawRectangle(5,5, 474, 266, Green);
	  LCD_ShowString(175, 50, STR_SMART_HOME, White, Black);		
		LCD_ShowString(125, 100, STR_CURTAIN_TITLE, Red, Black);	
    LCD_ShowString(150, 150, STR_CURRENT_LUX, Red, Black);
	  LCD_ShowString(160, 225, STR_S3_NEXT, White, Black); 

    I2C_GPIO_Configuration();                                           //  配置I2C的GPIO
    StepMotorGpio_Init();                                               //  初始化步进电机引脚
	  BH1750_Init();                                                      //  初始化BH1750
    
	  while(1)
	  {    
        BH1750_ReadData(BH1750_SlaveAddress, Databuf);
        
        temp = Databuf[0];
        temp = (temp << 8) + Databuf[1];
        date = temp*10/12;
	
        sprintf(Displaybuf, "%5u Lux", date);
        
        LCD_ShowString(300, 150, (unsigned char*)Displaybuf, White, Black);
        lux=atoi(Displaybuf);
			
				if(lux>1000)                                                  //LUX大于1000，过于明亮，窗帘下拉
				{
					xiajiantou();
					LCD_ShowString(200, 200, STR_CURTAIN_DOWN, Red, Black);
					StepMotorTest(0);
			 
				//  按下按键S5后退出测试
           if (KeyScan() == 5)
          {
               lcd_clear(Black);
               delay_ms(500);
               return;            
           }
				}
				
				else if(lux<5)                                                   //LUX小于5，光线过暗，窗帘上拉
				{
					shangjiantou();
					LCD_ShowString(200, 200, STR_CURTAIN_UP, Red, Black);
					StepMotorTest(1);
				 
				//  按下按键S5后退出测试
           if (KeyScan() == 5)
           {
               lcd_clear(Black);   
               delay_ms(500);    
               return;            
           }
				}
				
				else if(5<=lux && lux<=1000)                                  //5<=lux && lux<=1000，正常亮度，窗帘静止
				{
					LCD_ShowString(200, 200, STR_CURTAIN_STATIC, Red, Black);
					if (KeyScan() == 5)
           {
              lcd_clear(Black);  
              delay_ms(500); 
              return;            
           }
				}
	  }
}

extern unsigned char  startflag;
extern unsigned short Speed;
extern unsigned char  Flag_1s;
/*********************************************************************************************************
温控风扇模块：通过温湿度传感器中的温度值控制风扇转动
*********************************************************************************************************/
void DHT11_test(void)
{    
    unsigned char temperature;          
    unsigned char humidity;     
    char buf[20] = "";
    
    unsigned short num = 0;
    // 未使用的变量，已注释掉
    // unsigned char buff[5] = {0};
    // int i=0;
    // int j=0;
      
    long Databuf[2] = {0};
    char Dispbuf[50] = "";

    lcd_clear(Black);
    LCD_DrawRectangle(0,0, 479, 271, Green);
    LCD_DrawRectangle(5,5, 474, 266, Green);
    LCD_ShowString(175, 50, STR_SMART_HOME, White, Black);	   
    LCD_ShowString(170, 100, STR_FAN_TITLE, Red, Black);    
    LCD_ShowString(160, 225, STR_S2_NEXT, White, Black); 
        
    DCMotorGpio_Init();                                                 //  初始化直流电机引脚
    I2C_GPIO_Configuration();                                           //  I2C引脚配置
    InitBMP180();                                                       //  初始化BMP085
    while(1)
    {
        DTH11_ReadData(&temperature, &humidity);                        //  读取温湿度值    
        sprintf(buf, STR_TEMPERATURE, temperature);
        LCD_ShowString(100, 150, (unsigned char*)buf, Red, Black);
        
        sprintf(buf, STR_HUMIDITY, humidity);
        LCD_ShowString(200, 150, (unsigned char*)buf, Red, Black);
       
        if(temperature>28)                             // 温度超过28度打开风扇
        {
            startflag = 1;
        }
        else if(temperature<26)                        // 温度低于26度关闭风扇
        {
						 LCD_ShowString(200, 200, STR_FAN_OFF, Red, Black);
             startflag = 0;
             buf[0] = '0';
             buf[1] = '0';
             buf[2] = '0';
             buf[3] = '0';
             buf[4] = '0';   
             DCMOTOR_A_L;
             DCMOTOR_B_L;  
        }

        if(Flag_1s == 1)
         {
            num = 100;            
            num = 60 * (Speed / 4);
            Speed = 0;
 
            buf[0] = num / 10000 + '0';
            buf[1] = num % 10000 / 1000 + '0';
            buf[2] = num % 1000 / 100 + '0';
            buf[3] = num % 100 / 10 + '0';
            buf[4] = num % 10 + '0';                
						
            LCD_ShowString(200, 200, STR_FAN_ON, Red, Black);
            Flag_1s = 0;
         }
				 
         BMP180Convert(Databuf);     				 //  气压计转换函数
				 
         sprintf(Dispbuf, STR_PRESSURE, Databuf[1]/1000.0);
         LCD_ShowString(300, 150, (unsigned char *)Dispbuf, Red, Black); 
                
        //  按下按键S3后退出测试
        if (KeyScan() == 3)
        {
            lcd_clear(Black);
            delay_ms(500);  
					
            return;       
					
        }
    }
}

/*********************************************************************************************************
天气预报模块：红绿黄小灯分别响应雨天、晴朗、阴天等天气状况
*********************************************************************************************************/
void TrafficLight_test(void)
{  
    unsigned char temp = 0;
    
    lcd_clear(Black);
	  LCD_DrawRectangle(0,0, 479, 271, Green);
	  LCD_DrawRectangle(5,5, 474, 266, Green);
	  LCD_ShowString(175, 50, STR_SMART_HOME, White, Black);	
	  LCD_ShowString(125, 100, STR_WEATHER_TITLE, Red, Black);	
    LCD_ShowString(125, 150, STR_GREEN_SUNNY, Green, Black); 
	  LCD_ShowString(225, 150, STR_YELLOW_CLOUDY, Yellow, Black); 
	  LCD_ShowString(325, 150, STR_RED_RAINY, Red, Black); 
  	LCD_ShowString(170, 220, STR_S1_END, White, Black); 
    
    TrafficLightGpio_Init();
    delay_ms(5000);
	  lcd_clear(Black);
	
	  while(1)
	   {      
		    temp = TrafficLight_Run();      
        
        if (temp == 1)
        {
            lcd_clear(Black);
            delay_ms(500);
					
            return;
					
        }
     }
}

